package com.asset.salesforce.activities;

import com.asset.salesforce.R;
import com.asset.salesforce.handlers.MenuHandler;
import com.mobsandgeeks.saripaar.Rule;
import com.mobsandgeeks.saripaar.Validator;
import com.mobsandgeeks.saripaar.Validator.ValidationListener;
import com.mobsandgeeks.saripaar.annotation.Email;
import com.mobsandgeeks.saripaar.annotation.Password;
import com.mobsandgeeks.saripaar.annotation.Required;
import com.mobsandgeeks.saripaar.annotation.TextRule;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;
public class LoginActivity extends Activity {

	@Required(order = 1,message="Please,Enter Your Email")
	@Email(order = 2,message="Please,Enter a valid Email address")
	private EditText txtEmail;
	
	@Password(order = 3,message="Please,Enter Your Password")
	@TextRule(order = 4, minLength = 6, message = "Enter at least 6 characters.")
	private EditText txtPassword;
	private Validator validator;
	private Button btnLogin;
	private RelativeLayout loginLayout;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		loginLayout = (RelativeLayout) findViewById(R.id.loginLayout);
		loginLayout.setOnTouchListener(new OnTouchListener()
		{
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				hideKeyboard(v);
		        return false;
			}
		});
		txtEmail = (EditText) findViewById(R.id.txtEmail);
		txtPassword = (EditText) findViewById(R.id.txtPassword);
        btnLogin = (Button) findViewById(R.id.btnLogin);
      //Listening to button event
        
        validator = new Validator(this);
        validator.setValidationListener(new ValidationListener() {
			@Override
			public void preValidation() {}
			
			@Override
			public void onValidationCancelled() {}
			@Override
			public void onSuccess() {
				Toast.makeText(LoginActivity.this, "You Have Successfully Loged in", Toast.LENGTH_SHORT).show();
				Intent mainActivity = new Intent(getApplicationContext(), MainActivity.class);
				startActivity(mainActivity);
			}
			@Override
			public void onFailure(View failedView, Rule<?> failedRule) {
				String message = failedRule.getFailureMessage();
				
		        if (failedView instanceof EditText) {
		        	EditText txt = (EditText) failedView;
		        	txt.requestFocus();
		        	txt.setError(message);
		        	
		        }
			}
		});	
        
        btnLogin.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
            		validator.validate();
           }
        });
	}
	protected void hideKeyboard(View view)
	{
	    InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
	    in.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.login, menu);
		return true;
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		//MenuActivity menuActivity= new MenuActivity(this);
		//menuActivity.onOptionsItemSelected(item);
		MenuHandler.handleOnItemSelected(item, this);
		return super.onOptionsItemSelected(item);
	}
}
